#ifndef GLCHILD_H
#define GLCHILD_H

#include <QtOpenGL/QGLWidget>
#include "arcball/Camera.h"
#include "arcball/Viewport.h"
#include "arcball/ArcBall.h"
#include "../controller/controller.h"
#include "../gl2ps.h"

enum MATERIAL
{
    MATERIAL_SILVER,
    MATERIAL_GOLD,
    MATERIAL_JADE,
    MATERIAL_LIGHTBLUE,
    MATERIAL_EMERALD,
    MATERIAL_POLISHEDSILVER,
    MATERIAL_CHROME,
    MATERIAL_COPPER,
    MATERIAL_POLISHEDGOLD,
    MATERIAL_PEWTER,
    MATERIAL_OBSIDIAN,
    MATERIAL_BLACKPLASTIC,
    MATERIAL_POLISHEDBRONZE,
    MATERIAL_POLISHEDCOPPER,
    MATERIAL_PEARL,
    MATERIAL_RUBY,
    MATERIAL_TURQUOISE,
    MATERIAL_BRASS,
    MATERIAL_END
};

extern QString materialName[MATERIAL_END];

class GLChild : public QGLWidget, public Controller
{
    Q_OBJECT

public:
    GLChild();
    ~GLChild();

    void newFile();
    virtual bool loadFile(const QString &fileName);
    bool save();
    bool saveAs();
    bool saveFile(const QString &fileName);
    QString userFriendlyCurrentFile();
    QString currentFile() { return curFile; }

    void enableLighting();

    virtual void resetView();

    int getDrawFlag(){return drawFlag;}

    bool getLightingFlag() {return lighting;}

    void OnMaterials(int nID);
    void ChangeMaterial(QString str, bool update);

protected:
    virtual void closeEvent(QCloseEvent *event);
    virtual void initializeGL();
    virtual void resizeGL(int w, int h);
    virtual void paintGL();
    void drawOverpaint(QPainter *painter);

    virtual void mousePressEvent(QMouseEvent * event);
    virtual void mouseMoveEvent(QMouseEvent * event);
    virtual void mouseReleaseEvent(QMouseEvent *event);
    virtual void wheelEvent(QWheelEvent * event);

protected slots:
    void documentWasModified();

protected:
    bool maybeSave();
    void setCurrentFile(const QString &fileName);
    QString strippedName(const QString &fullFileName);
    void drawSelectingBox();

    // gl2ps
    void writefile(int format, int sort, int options, int nbcol,
                   const char *filename, const char *extension);

    // camera
    void InitCamera();
    void ViewAll(bool check_first = true);

    // mouse
    void HandleMouseButton(double x, double y);

    int winWidth, winHeiht;

    bool m_FirstView;

    // mouse
    bool leftButtonDown;
    bool rightButtonDown;

    bool lighting;

    // arcball
    CCamera   camera;
    CViewport viewport;
    CArcball  arcball;

    QString curFile;
    bool isUntitled;

    int drawFlag;

    QPrinter * printer;
};

#endif
